<?php


namespace App\Logic;


use App\Models\Goods;
use App\server\EsServer;


/**
 * kys
 * Class GoodsLogic
 * @package App\Logic
 */
class GoodsLogic
{

    /**
     * 商品列表展示
     * @return mixed
     */
    public static function GoodsList()
    {
        $goods = Goods::list();
        return $goods;
    }

    /**
     * 商品添加
     * @return mixed
     * kys
     */
    public static function GoodsAdd($request)
    {
        $goodsModel = Goods::create($request);
        return $goodsModel;
    }

    /**
     * 商品信息删除
     * @param $goodsId
     * @return mixed
     * @throws \Exception
     */

    public static function GoodsDel($goodsId)
    {
        if (empty($goodsId)) throw new \Exception('未收到删除商品信息');
        $executeDel = Goods::del('id', $goodsId);
        return $executeDel;
    }

    /**
     * 商品信息查找
     */
    public static function GoodsSeek($query)
    {
        if (empty($query)) throw new \Exception('未搜索到商品信息');
        $executeSeek = Goods::Seek('goods_name', $query);
        return $executeSeek;
    }

    /**
     * 商品信息详情
     */
    public static function GoodsDetails($goodsId)
    {
        if (empty($goodsId)) throw new \Exception('未接收到想要查看的商品信息');
        $executeDetail = Goods::detail('id', $goodsId);
        return $executeDetail;
    }

    /**
     * 商品信息修改
     */
    public static function GoodsUpdated($SecondaryMessageUpdated)
    {
        unset($SecondaryMessageUpdated['updated_at']);
        unset($SecondaryMessageUpdated['created_at']);
        unset($SecondaryMessageUpdated['deleted_at']);
        $goodsId = $SecondaryMessageUpdated['id'];
        $SecondaryModel = Goods::change('id', $goodsId, $SecondaryMessageUpdated);
        return $SecondaryModel;
    }

    /**
     * 二手房源es搜索
     */
    public static function SecondarySearch($query)
    {
        if (empty($query)) {
            return Goods::list();
        }
        $executeEsSearch = new EsServer();
        $EsMessage['data'] = $executeEsSearch->searchEs('goods', $query, 'goods_name');
        return $EsMessage;
    }

}
